// ------------------------------------------------------------------------------------------------------------------------
// Plemiona.pl 5.X Skrypt użytkowy
//
// Nazwa:      Piękny widok przeglądu surowców
// Wersja:     1.2.0
// Autor:      Domanoid (Forum Plemion)
// Dostosował: Lukasz032 (Plemiona Ś7)
//
// Tagi specjakne:
// Licencja:   Creative Commons Uznanie autorstwa - Brak komercyjnego zastosowania - Na tych samych warunkach 2.0 Polska
// Informacje: http://creativecommons.org/licenses/by-nc-sa/2.0/pl
// Support:    lvwnbrz@lykamspam.pl
//
// ------------------------------------------------------------------------------------------------------------------------
// ==UserScript==
// @name           Piękny widok przeglądu surowców
// @namespace      http://code.google.com/p/plemiona-skrypty/
// @description    Piękny widok przeglądu surowców
// @version        1.2.0 PL
// @license        Creative Commons 2.0 BY-NC-SA (http://creativecommons.org/licenses/by-nc-sa/2.0/pl)
// @author         Domanoid (Forum Plemion)
// @contributor    Lukasz032 (Plemiona Ś7)
// @include        http://pl*.plemiona.pl/*
// ==/UserScript==
// ------------------------------------------------------------------------------------------------------------------------



// Aktualnie zainstalowana wersja:
var vers_ist = "Piękny Widok 1.2.0";



// Pobiera informacje z:
var url = document.location.href;


// Świat:
var teil = url.split(".");
var welt = teil[0].replace("http://pl", "");


// ID Wioski:
var nummer = url.split("village=");
var numm = nummer[1].split("&");
var dorf_ID = numm[0];


// Obsługa IE \ Opery, czyli przekierowanie odczytu i zapisu do cookies oraz styli do warstwy window.
// GMS-To-Cookies-Elementary-2 by Staemmes-Anonim

 if(typeof GM_addStyle!="function"){
  function GM_setValue(name, value) {
		document.cookie = name + '=' + escape(value) + '; expires=' + (new Date(2020, 12, 1)).toGMTString() + ';';
	}
	function GM_getValue(name) {
		var value = document.cookie.match('/'+name+'=(.*?)(?:;|$)/');
		if(value)
		return unescape(value[1]);
		return false;
	}
	function GM_addStyle(style) {
		var styleel = document.createElement('style');
		styleel.setAttribute('type', 'text/css');
		styleel.innerHTML = style;
		document.getElementsByTagName('head')[0].appendChild(styleel);
	}
 }
 if(!unsafeWindow) {
    unsafeWindow = window
 } 
 
 
// Walidator uprawnień uruchamiania.... 
  function getGameDoc() {
  	getdoc=window.document;
  	if(!getdoc.URL.match("game.php")){
  		for(zeilenNummer=0;zeilenNummer<window.frames.length;zeilenNummer++){
  			if(window.frames[zeilenNummer].document.URL.match("game.php")){
  				getdoc = window.frames[zeilenNummer].document;
	  		}
  		}
  	}
  	return getdoc;
  }

  var PA = false;
  var doc = getGameDoc();
  try{if(doc.getElementsByTagName("ul")[0].className=="menu nowrap quickbar"){PA=true;}}
  catch(evt){}


// Ustawienia:
if(url.match(/screen=settings&mode=settings/)) {
	var vers = vers_ist.split(" ");
	var version = "";
	for(v=0; v<vers.length; v++) {
		if(v < vers.length-1) {
			version += vers[v] + " ";
		}
		else {
			version += "<span class='grey'>" + vers[v] + "</span>";
		}
	}
	
	// Ustawienia (Przegląd surowców):
	if(url.match(/einstellung=speicher/)) {
		// Nagłówek jako procenty (tak/nie):
		if(confirm("Nagłówek:\n\nPokazywać w nagłówku procenty dla surowców i zagrody?")) {
			GM_setValue("Speicher-Top-" + welt, "an");		
		}
		else {
			GM_setValue("Speicher-Top-" + welt, "aus");		
		}
		// Zapisywać id wioski jako wartość GM?:
		if(confirm("Aktualna wioska:\n\nPokazywać w przeglądzie wiosek (z KP) aktualną wioskę kolorowo?")) {
			GM_setValue("Speicher-Akt-" + welt, "an");		
		}
		else {
			GM_setValue("Speicher-Akt-" + welt, "aus");		
		}
		// Poparcie jako wartość GM?:
		if(confirm("Poparcie:\n\nPokazywać w przeglądzie wiosek (z KP) poparcie dla pojedynczych wiosek?")) {
			GM_setValue("Speicher-Zus-" + welt, "an");		
		}
		else {
			GM_setValue("Speicher-Zus-" + welt, "aus");		
		}
		// Monety oraz szlachta jako GM-Value?:
		if(confirm("Złoto / Szlachta:\n\nPokazywać możliwą do wyprodukowania liczbę monet i szlachciców?")) {
			GM_setValue("Speicher-GAG-" + welt, "an");		
		}
		else {
			GM_setValue("Speicher-GAG-" + welt, "aus");		
		}
		// Zagroda %:
		if(confirm("Zagroda %:\n\nPokazywać ilość miejsc w zagrodzie procentowo i kolorowo?")) {
			GM_setValue("Speicher-Bau-" + welt, "an");		
		}
		else {
			GM_setValue("Speicher-Bau-" + welt, "aus");		
		}
		// Inne kolory dla daltonistów:
		if(confirm("Normalne kolory:\n\nPokazywać kolory od zielonego do czerwonego?\n\nJeśli jesteś daltonistą i nie widzisz różnicy pomiędzy czerwonym a zielonym, powinieneś kliknąć na anuluj.\nWtedy kolory zostaną ustawione od żółtego (0%) do niebieskiego (100%)")) {
			GM_setValue("Speicher-Blind-RG-" + welt, "aus");		
		}
		else {
			GM_setValue("Speicher-Blind-RG-" + welt, "an");		
		}
	}
	
	var tr = new Array();
	tr[0] = document.createElement("tr");
	tr[1] = document.createElement("tr");
	var th = new Array();
	th[0] = document.createElement("th");
	th[1] = document.createElement("th");
	var td = new Array();
	td[0] = document.createElement("td");
	td[1] = document.createElement("td");
	
	th[0].setAttribute("colspan", "2");
	th[0].innerHTML = "<a href='http://forum.plemiona.pl/showthread.php?t=58684' target='Forum' title='Do tematu na forum \"" + version.split(" <span")[0] + "\" przejść'>" + version + "</a>";
	td[0].setAttribute("style", "vertical-align:top;");
	
	if((GM_getValue("Speicher-Top-" + welt) == undefined) && 
		(GM_getValue("Speicher-Akt-" + welt) == undefined) && 
		(GM_getValue("Speicher-Zus-" + welt) == undefined) && 
		(GM_getValue("Speicher-GAG-" + welt) == undefined) && 
		(GM_getValue("Speicher-Bau-" + welt) == undefined) && 
		(GM_getValue("Speicher-Blind-RG-" + welt) == undefined)) {
		td[0].innerHTML = "<a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_ID + "&screen=settings&mode=settings&einstellung=speicher''>Zapisać ustawienia</a>";
		td[1].innerHTML = "<span class='grey'>Jeszcze nie zostały zapisane ustawienia</span>";
	}
	else {
		td[0].innerHTML = "<a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_ID + "&screen=settings&mode=settings&einstellung=speicher''>Zmienić ustawienia</a>";
		var speicher_top = GM_getValue("Speicher-Top-" + welt);
		var speicher_akt = GM_getValue("Speicher-Akt-" + welt);
		var speicher_zus = GM_getValue("Speicher-Zus-" + welt);
		var speicher_gag = GM_getValue("Speicher-GAG-" + welt);
		var speicher_bau = GM_getValue("Speicher-Bau-" + welt);
		var speicher_rgb = GM_getValue("Speicher-Blind-RG-" + welt);
		
		td[1].innerHTML += "<b style='padding-right:19px;'>Nagłówek %:</b>" + speicher_top.replace("an", "<span style='color:#090'>tak</span>").replace("aus", "<span style='color:#C00'>nie</span>") + "<br>";
		td[1].innerHTML += "<b style='padding-right:5px;'>Podświetlenie wioski:</b>" + speicher_akt.replace("an", "<span style='color:#090'>tak</span>").replace("aus", "<span style='color:#C00'>nie</span>") + "<br>";
		td[1].innerHTML += "<b style='padding-right:19px;'>Poparcie:</b>" + speicher_zus.replace("an", "<span style='color:#090'>tak</span>").replace("aus", "<span style='color:#C00'>nie</span>") + "<br>";
		td[1].innerHTML += "<b style='padding-right:32px;'>Złoto i szl.:</b>" + speicher_gag.replace("an", "<span style='color:#090'>tak</span>").replace("aus", "<span style='color:#C00'>nie</span>") + " <span class='grey>ab Welt 3</span><br>";
		td[1].innerHTML += "<b style='padding-right:12px;'>Zagroda %:</b>" + speicher_bau.replace("an", "<span style='color:#090'>tak</span>").replace("aus", "<span style='color:#C00'>nie</span>") + "<br>";
		td[1].innerHTML += "<b style='padding-right:21px;'>Daltonizm:</b>" + speicher_rgb.replace("an", "<span style='color:#090'>tak</span> <span class='grey>Żółty (0%) &raquo; Niebieski (100%)</span>").replace("aus", "<span style='color:#C00'>nie</span> <span class='grey>Zielony (0%) &raquo; Czerwony (100%)</span>");
	}
	
	tr[0].appendChild(th[0]);
	tr[1].appendChild(td[0]);
	tr[1].appendChild(td[1]);
	
	document.getElementsByClassName("main")[0].getElementsByTagName("table")[2].appendChild(tr[0]);
	document.getElementsByClassName("main")[0].getElementsByTagName("table")[2].appendChild(tr[1]);
}
else {
	if((url.match(/screen=/)) && 
		(GM_getValue("Speicher-Top-" + welt) == undefined) && 
		(GM_getValue("Speicher-Akt-" + welt) == undefined) && 
		(GM_getValue("Speicher-Zus-" + welt) == undefined) && 
		(GM_getValue("Speicher-GAG-" + welt) == undefined) && 
		(GM_getValue("Speicher-Bau-" + welt) == undefined) && 
		(GM_getValue("Speicher-Blind-RG-" + welt) == undefined)) {
		if(confirm(vers_ist + " jest aktualnie zainstalowy\n\nKliknij na OK aby przejść do ustawień")) {
			document.location.href = "http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_ID + "&screen=settings&mode=settings&einstellung=speicher";
		}
		else {
			// Zapisywanie jako GM-Value
			GM_setValue("Speicher-Top-" + welt, "an");
			GM_setValue("Speicher-Akt-" + welt, "an");
			GM_setValue("Speicher-Zus-" + welt, "an");
			GM_setValue("Speicher-GAG-" + welt, "an");
			GM_setValue("Speicher-Bau-" + welt, "an");
			GM_setValue("Speicher-Blind-RG-" + welt, "aus");
		}
	}
}




function vlgrvpatch() {

 // Walidacja KP....
 if (!PA) {
 return false
 }

 // KP-Info:
 var info = 0;

 
 // Używać tylko w przeglądzie wiosek:
 if(url.match(/screen=overview$/)) {
 	var vis = document.getElementsByClassName("vis").length-1;
 	var Stunde = new Date().getHours();
 	var test = "";
 	var zustimmung = 100;
 	for(v=0; v<=vis; v++) {
 		test = document.getElementsByClassName("vis")[v].getElementsByTagName("th")[0].innerHTML;
 		if(test.match(/Poparcie/)) {
 			zustimmung = document.getElementsByClassName("vis")[v].getElementsByTagName("th")[1].innerHTML;
 		}		
 	}
 	
 	GM_setValue("Stunde-" + dorf_ID, Stunde);
 	GM_setValue("Zustimmung-" + dorf_ID, zustimmung);
 }
 
 // Nagłówek:
 if(GM_getValue("Speicher-Top-" + welt) == "an") {
	// Procenty w nagłówku dla aktualnych surowców:
	var akt_holz = document.getElementById("wood").innerHTML;
	var akt_lehm = document.getElementById("stone").innerHTML;
	var akt_eisen = document.getElementById("iron").innerHTML;
	var akt_speicher = document.getElementById("storage").innerHTML;
	
	var pro_holz = Math.round((100/akt_speicher)*akt_holz);
	var pro_lehm = Math.round((100/akt_speicher)*akt_lehm);
	var pro_eisen = Math.round((100/akt_speicher)*akt_eisen);
	
	document.getElementById("wood").innerHTML += "<span class='grey' style='margin-left:5px;'>(" + pro_holz + "%)</span>";
	document.getElementById("stone").innerHTML += "<span class='grey' style='margin-left:5px;'>(" + pro_lehm + "%)</span>";
	document.getElementById("iron").innerHTML += "<span class='grey' style='margin-left:5px;'>(" + pro_eisen + "%)</span>";
	
	
	var box = document.getElementsByClassName("box").length-1;
	var volk = document.getElementsByClassName("box")[box].innerHTML;

	if(volk.match(/att.png/)) {
		if(volk.match(/face.png/)) {
			box = box-1;
		}
		else {
			box = box-2;
		}
	}
	else {
		if(volk.match(/face.png/)) {
			box = box;
		}
		else {
			box = box-1;
		}
	}
	volk = document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML.split("/");
	
	var pro_volk = Math.round((100/volk[1])*volk[0]);
	
	// Od świata 3 pokazywać ilość możliwych monet i szlachty:
	// >>> TU JESZCZE DOPRACOWAĆ!!! <<<
	if(((welt == 3) || (welt == 4) || (welt == 5) || (welt == 6) || (welt == 7) || (welt == 8) || (welt == 9) || (welt == 11) || (welt == 12) || (welt == 13) || (welt == 14) || (welt == 16) || (welt == 17) || (welt == 18) || (welt == 19) || (welt == 20) || (welt == 22) || (welt == 25) || (welt == 26) || (welt == 29) || (welt == 31) || (welt == 32) || (welt == 33)) && (GM_getValue("Speicher-GAG-" + welt) == "an")) {
		var gold_holz = akt_holz - (akt_holz%28000);
		var gold_lehm = akt_lehm - (akt_lehm%30000);
		var gold_eisen = akt_eisen - (akt_eisen%25000);
	
		var snob_holz = akt_holz - (akt_holz%40000);
		var snob_lehm = akt_lehm - (akt_lehm%50000);
		var snob_eisen = akt_eisen - (akt_eisen%50000);
		
		if((welt == 3) || (welt == 9)) {
			var gold_preis_holz = 20000;
			var gold_preis_lehm = 30000;
			var gold_preis_eisen = 25000;
			var snob_preis_holz = 20000;
			var snob_preis_lehm = 30000;
			var snob_preis_eisen = 25000;
		}
		else if((welt >= 4) && (welt <= 8)) {
			var gold_preis_holz = 28000;
			var gold_preis_lehm = 30000;
			var gold_preis_eisen = 25000;
			var snob_preis_holz = 28000;
			var snob_preis_lehm = 30000;
			var snob_preis_eisen = 25000;
		}
		else if(welt >= 10) {
			var gold_preis_holz = 28000;
			var gold_preis_lehm = 30000;
			var gold_preis_eisen = 25000;
			var snob_preis_holz = 40000;
			var snob_preis_lehm = 50000;
			var snob_preis_eisen = 50000;
		}
	
		var gold = Math.min(Math.min(gold_holz/gold_preis_holz,gold_lehm/gold_preis_lehm),gold_eisen/gold_preis_eisen);
		var snob = Math.min(Math.min(snob_holz/snob_preis_holz,snob_lehm/snob_preis_lehm),snob_eisen/snob_preis_eisen);
	
	
		document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML += "<span class='grey' style='margin-left:5px;'>(" + pro_volk + "%)</span>";
		
		if(welt >= 10) {
			document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML += " <img src='http://pl" + welt + ".plemiona.pl/graphic/gold.png' style='height:14px; cursor:help;' title='W tej wiosce możesz jeszcze  " + gold + " monet wybić'> " + gold;
		}
		else {
			document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML += " <img src='http://pl" + welt + ".plemiona.pl/graphic/res.png' style='height:14px; cursor:help;' title='W tej wiosce możesz jeszcze " + gold + " x surków odłóżyć.'> " + gold;
		}
	
		if(snob > 0) {
			var volk_rest = (volk[1] - volk[0]);
			var volk_soll = (snob * 200);
			
			var max_snob = Math.round(volk_rest/200);
	
			var plus_snob = "";
			if(max_snob > 1) {
				plus_snob = "er";
			}
	
			if(max_snob >= 1) {
				snob_gesamt_2 += snob;
				if(max_snob < snob){
					document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML += " <img src='http://pl" + welt + ".plemiona.pl/graphic/unit/unit_snob.png' style='height:14px; cursor:help;' title='W tej wiosce możesz jeszcze " + max_snob + " z " + snob + " szlachciców wyprodukować'> <a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_ID + "&screen=snob'>" + max_snob + "</a><span class='grey'> /" + snob + "</span>&nbsp;";
				}
				else {
					document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML += " <a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_ID + "&screen=snob'> <img src='http://pl" + welt + ".plemiona.pl/graphic/unit/unit_snob.png' style='height:14px; cursor:help;' title='W tej wiosce możesz jeszcze " + snob + " szlachciców" + plus_snob + " wyprodukować'> " + snob + "</a><span class='grey'>/" + snob + "</span>&nbsp;";
				}
			}
			else {
				document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML += " <span class='grey' title='Zagroda w tej wiosce nie zmieści więcej jednostek'><img src='http://pl" + welt + ".plemiona.pl/graphic/unit/unit_snob.png' style='height:14px; cursor:help;'> 0/" + snob + "</span>&nbsp;";
			}
		}
		else {
			document.getElementsByClassName("box")[box].getElementsByTagName("td")[1].innerHTML += " <span class='grey' title='W tej wiosce nie można wyprodukować więcej szlachciców'> <img src='http://pl" + welt + ".plemiona.pl/graphic/unit/unit_snob.png' style='height:14px; cursor:help;'> " + snob + "</span>&nbsp;";
		}
	}
 }






 // Tylko w przeglądzie wiosek:
 if(url.match(/screen=overview_villages/)) {

	var test1 = "";
	var test2 = "";

	test1 = document.getElementById("overview");


	// Bez KP:   
	if(test1 == null) {
		return false;
	}
	// Z KP:   
	else {
		test2 = document.getElementsByClassName("main")[0].getElementsByClassName("vis")[0].getElementsByClassName("selected")[0].getElementsByTagName("a")[0].innerHTML;
		if(test2 == "Produkcja") {
			info = 2;
		}
	}


   	if(info >= 1) {




		// Wyznaczyć ilość tabel:
		var main = document.getElementsByClassName("main").length-1;
		var vis = document.getElementsByClassName("main")[main].getElementsByTagName("table").length;
		var kopf = "";


		// Wybór tabeli:
		if(info == 1) {
			if(vis > 1) {
				vis = vis -2;
			}
			else {
				vis = 0;
			}
		}


		if(info == 2) {
			if(vis >= 4) {
				vis = vis -2;
			}
			else {
				vis = vis -1;
			}
		}


		// Wyznaczyć ilość kolumn:
		var spalten = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[0].getElementsByTagName("th").length;

		// Wyznaczyć ilość wierszy:
		var zeilen = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr").length;
		// header:
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'>Wioska</th>";
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default;'>Punkty</th>";
		if(GM_getValue("Speicher-Zus-" + welt) == "an") {
			kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topright:10px;'><img src='http://pl8.plemiona.pl/graphic/ally_rights/lead.png' title='Poparcie'></th>";
		}

		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'><img src='/graphic/holz.png' title='Drewno' alt='' /></th><th style='text-align:center; cursor:default; -moz-border-radius-topright:10px;'>Drewno</th>";
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'><img src='/graphic/lehm.png' title='Glina' alt='' /></th><th style='text-align:center; cursor:default; -moz-border-radius-topright:10px;'>Glina</th>";
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'><img src='/graphic/eisen.png' title='Żelazo' alt='' /></th><th style='text-align:center; cursor:default; -moz-border-radius-topright:10px;'>Żelazo</th>";
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'><img src='/graphic/res.png' title='Spichlerz' alt='' /></th>";
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topright:10px;'>Max.</th>";
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'><img src='/graphic/face.png' title='Zagroda' alt='' /></th>";
		kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topright:10px;'>Zagroda</th>";
		if((welt >= 3) && (GM_getValue("Speicher-GAG-" + welt) == "an")) {
			if(welt >= 10) {
				kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'><img src='/graphic/gold.png' title='Monety' alt='' /></th>";
			}
			else {
				kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topleft:10px;'><img src='/graphic/res.png' title='Odłożenie surowców' alt='' /></th>";
			}
			kopf += "<th style='text-align:center; padding-left:5px; padding-right:5px; cursor:default; -moz-border-radius-topright:10px;'><img src='/graphic/unit/unit_snob.png' title='Szlachta' alt='' /></th>";
		}
		if(info == 2) {
			kopf += "<th style='text-align:center; padding:2px; cursor:default; -moz-border-radius-topleft:10px;'>Budowa</th>";
			kopf += "<th style='text-align:center; padding:2px; cursor:default;'>Wynalazki</th>";
			kopf += "<th style='text-align:center; padding:2px; -moz-border-radius-topright:10px; cursor:default;'>Rekrutacja</th>";
		}
		// aktualizacja header:
		document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[0].innerHTML = kopf;   

		var holz_gesamt = 0;
		var lehm_gesamt = 0;
		var eisen_gesamt = 0;
		var speicher_gesamt = 0;
		var volk_gesamt_1 = 0;
		var volk_gesamt_2 = 0;
		var gold_gesamt = 0;
		var snob_gesamt_1 = 0;	
		var snob_gesamt_2 = 0;	

		// Wiersze:
		for(i=1; i<=zeilen; i++) {
			// Nowy wiersz:
			var zeile = "";   
			// Wyczytuje wartości z wierszów:
			var dorf = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[0].innerHTML;
			var dorf_link = dorf.split("village=");
			var dorf_id = dorf_link[1].split("&");
			
			var punkte = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[1].innerHTML;
			if(GM_getValue("Zustimmung-" + dorf_id[0]) != undefined) {
				var zustimm = GM_getValue("Zustimmung-" + dorf_id[0]);
				if(zustimm != 100) {
					var std_1 = GM_getValue("Stunde-" + dorf_id[0]);
					var std_2 = new Date().getHours();
					var stunden = std_2 - std_1;
					zustimm = (GM_getValue("Zustimmung-" + dorf_id[0])*1)+(stunden*1);
				}
				if(zustimm <= 100) {
					var zustimmung = zustimm;
				}
				else {
					var zustimmung = 100;
				}
			}
			else {
				var zustimmung = "<span style='color:silver; cursor:help;' title='Poparcie dla tej wioski nie zostało jeszcze zapisane'>100</span>";
			}
			var rohstoffe = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[2].innerHTML;
			var rohstoff = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[2].innerHTML.split("<img");
			var speicher = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[3].innerHTML;
			var bauernhof = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[4].innerHTML.split("/");
			if(info == 2) {
				for(z=5; z<=7; z++) {
					var bilder = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[z].getElementsByTagName("img").length;
					for(b=0; b<bilder; b++) {
						document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[z].getElementsByTagName("img")[b].setAttribute("style", "height:14px; margin-left:2px;");	
					}
				}
				
				var ba = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[5].innerHTML.split("<br>")[1];
				if(ba != undefined) {
					var bauauftrag = "<small> " + ba + "</small>";
				}
				else {
					var bauauftrag = "";
				}
				var forschung = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[6].innerHTML;
				var rekrutierung = document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].getElementsByTagName("td")[7].innerHTML;
			}

   
			// Rozkład wartości:
			var holz = rohstoff[1].split("\"\">");
			var lehm = rohstoff[2].split("\"\">");
			var eisen = rohstoff[3].split("\"\">");

   
			// Zamiana warości:
			var wert_h = holz[1];
			var wert_l = lehm[1];
			var wert_e = eisen[1];
   
			// Code by -- Tera
			wert_h = wert_h.replace(/SPAN/gi,"span");
			wert_l = wert_l.replace(/SPAN/gi,"span");
			wert_e = wert_e.replace(/SPAN/gi,"span");
			
			
			var w_h = wert_h.replace("<span class=\"grey\">.</span>", "").replace("<span class=\"warn\">", "").replace("</span>", "");
			var w_l = wert_l.replace("<span class=\"grey\">.</span>", "").replace("<span class=\"warn\">", "").replace("</span>", "");
			var w_e = wert_e.replace("<span class=\"grey\">.</span>", "").replace("<span class=\"warn\">", "").replace("</span>", "");

	   
			// Zamiana wartości:
			holz_gesamt += parseFloat(w_h);
			lehm_gesamt += parseFloat(w_l);
			eisen_gesamt += parseFloat(w_e);
			speicher_gesamt += parseFloat(speicher);
			volk_gesamt_1 += parseFloat(bauernhof[0]);
			volk_gesamt_2 += parseFloat(bauernhof[1]);
   
			// Kolory:


			// Poziomy kolorów:
			var stufen = 100;
			var teile = 100/stufen;
			var schritt = Math.round(255/stufen)-1;
			var hgFarbe = new Array();
			
			// Poziomy kol. dla daltonistów
			if(GM_getValue("Speicher-Blind-RG-" + welt) == "an") {
				var colorR = 255;
				var colorG = 255;
				var colorB = 0;
				
				for(c=0; c<=stufen; c++) {
					var farbeR = (colorR - schritt * c).toString(16);
					var farbeG = (colorG - schritt * c).toString(16);
					var farbeB = (colorB + schritt * c).toString(16);
	   
					if(farbeR.length == 1) {
						farbeR = "0" + farbeR;
					}
					if(farbeG.length == 1) {
						farbeG = "0" + farbeG;
					}
					if(farbeB.length == 1) {
						farbeB = "0" + farbeB;
					}
	
					hgFarbe[c] = "#" + farbeR + farbeG + farbeB;
				}
			}
			// Normalne kolory
			else {
				var colorR = 0;
				var colorG = 255;
				var colorB = 255;
				
				for(c=0; c<=stufen; c++) {
					var farbeR = (colorR + schritt * c).toString(16);
					var farbeG = (colorG - schritt * c).toString(16);
					var farbeB = "00"
					var farbeB = "00"
	   
					if(farbeR.length == 1) {
						farbeR = "0" + farbeR;
					}
					if(farbeG.length == 1) {
						farbeG = "0" + farbeG;
					}
	
					hgFarbe[c] = "#" + farbeR + farbeG + farbeB;
				}
			}


			// Przeliczenie procentów:
			var proz_h = Math.round((100/speicher)*w_h);
			var proz_l = Math.round((100/speicher)*w_l);
			var proz_e = Math.round((100/speicher)*w_e);
			var proz_v = Math.round((100/bauernhof[1])*bauernhof[0]);
			var proz_b = Math.round((100/24000)*bauernhof[0]);
	   
			var pro_h = Math.round(proz_h/teile);
			var pro_l = Math.round(proz_l/teile);
			var pro_e = Math.round(proz_e/teile);
			var pro_v = Math.round(proz_v/teile);

			var gold_holz = w_h - (w_h%28000);
			var gold_lehm = w_l - (w_l%30000);
			var gold_eisen = w_e - (w_e%25000);

			var snob_holz = w_h - (w_h%40000);
			var snob_lehm = w_l - (w_l%50000);
			var snob_eisen = w_e - (w_e%50000);

			if((welt == 3) || (welt == 9)) {
				var gold_preis_holz = 20000;
				var gold_preis_lehm = 30000;
				var gold_preis_eisen = 25000;
				var snob_preis_holz = 20000;
				var snob_preis_lehm = 30000;
				var snob_preis_eisen = 25000;
			}
			else if((welt >= 4) && (welt <= 8)) {
				var gold_preis_holz = 28000;
				var gold_preis_lehm = 30000;
				var gold_preis_eisen = 25000;
				var snob_preis_holz = 28000;
				var snob_preis_lehm = 30000;
				var snob_preis_eisen = 25000;
			}
			else if(welt >= 10) {
				var gold_preis_holz = 28000;
				var gold_preis_lehm = 30000;
				var gold_preis_eisen = 25000;
				var snob_preis_holz = 40000;
				var snob_preis_lehm = 50000;
				var snob_preis_eisen = 50000;
			}
		
			var gold = Math.min(Math.min(gold_holz/gold_preis_holz,gold_lehm/gold_preis_lehm),gold_eisen/gold_preis_eisen);
			var snob = Math.min(Math.min(snob_holz/snob_preis_holz,snob_lehm/snob_preis_lehm),snob_eisen/snob_preis_eisen);

			// Ponowne złożenie wierszy:
			zeile += "<td style='padding-left:5px; padding-right:5px;'>" + dorf + "</td>";
			zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:default;'>" + punkte + "</td>";
			if(GM_getValue("Speicher-Zus-" + welt) == "an") {
				zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:default;'><small>" + zustimmung + "</small></td>";
			}
			zeile += "<td style='cursor:help; text-align:right; padding-left:5px; color:#fff; background-color:" + hgFarbe[pro_h] + "' title='Drewno: " + proz_h + "%'><small>" + proz_h + "%</small></td>";
			zeile += "<td style='text-align:right; padding-right:5px; cursor:default;' title='Drewno: " + proz_h + "%'>" + wert_h + "</td>";
			zeile += "<td style='cursor:help; text-align:right; padding-left:5px; color:#fff; background-color:" + hgFarbe[pro_l] + "' title='Glina: " + proz_l + "%'><small>" + proz_l + "%</small></td>";
			zeile += "<td style='text-align:right; padding-right:5px; cursor:default;' title='Glina: " + proz_l + "%'>" + wert_l + "</td>";
			zeile += "<td style='cursor:help; text-align:right; padding-left:5px; color:#fff; background-color:" + hgFarbe[pro_e] + "' title='Żelazo: " + proz_e + "%'><small>" + proz_e + "%</small></td>";
			zeile += "<td style='text-align:right; padding-right:5px; cursor:default;' title='Żelazo: " + proz_e + "%'>" + wert_e + "</td>";

			zeile += "<td colspan='2' style='text-align:right; padding-left:5px; padding-right:5px; cursor:default;'>" + (speicher/1000 ).toFixed(3) + "</td>";
			
			var colspan = 2;
			if(GM_getValue("Speicher-Bau-" + welt) == "an") {
				zeile += "<td style='cursor:help; text-align:right; padding-left:5px; color:#fff; background-color:" + hgFarbe[pro_v] + "' title='Zagroda: " + proz_v + "% (aktualnie) | " + proz_b + "% (razem)'><small>" + proz_v + "%</small></td>";
				colspan = 1;
			}
			zeile += "<td colspan='" + colspan + "' style='text-align:right; padding-left:5px; padding-right:5px; cursor:default;'>" + (bauernhof[0]/1000 ).toFixed(3) + " <span class=\"grey\">/</span> " + (bauernhof[1]/1000 ).toFixed(3) + "</td>";

			if((welt >= 3) && (GM_getValue("Speicher-GAG-" + welt) == "an")) {
				var plus_gold = "";
				var plus_snob = "";
	
				if(gold = 1) {
					plus_gold = "ę";
				}
	
				if(welt >= 10) {
					if(gold > 0) {
						gold_gesamt += gold;
						zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='Możesz jeszcze " + gold + " monet" + plus_gold + " wybić'><a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_id[0] + "&screen=snob'>" + gold + "</a></td>";
					}
					else {
						zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='W tej chwili nie możesz wybić ani jednej monety'><span class='grey'>" + gold + "</span></td>";
					}
				}
				else {
					if(gold > 0) {
						gold_gesamt += gold;
						zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='Możesz jeszcze " + gold + " x surowców odłożyć'><a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_id[0] + "&screen=snob'>" + gold + "</a></td>";
					}
					else {
						zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='W tej chwili nie możesz odłożyć surowców'><span class='grey'>" + gold + "</span></td>";
					}
				}
				
				if(snob > 0) {
					snob_gesamt_1 += snob;
					var volk_rest = (bauernhof[1] - bauernhof[0]);
					var volk_soll = (snob * 200);
					
					var max_snob = Math.round(volk_rest/200);
	
					if(max_snob > 1) {
						plus_snob = "ów";
					}
		
					if(max_snob >= 1) {
						snob_gesamt_2 += snob;
						if(max_snob < snob){
							zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='Możesz " + max_snob + " z " + snob + " szlachciców wyprodukować'><a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_id[0] + "&screen=snob'>" + max_snob + "</a><span class='grey'>/" + snob + "</span></td>";
						}
						else {
							zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='Możesz " + snob + " szlachcic" + plus_snob + " wyprodukować'><a href='http://pl" + welt + ".plemiona.pl/game.php?village=" + dorf_id[0] + "&screen=snob'>" + snob + "</a><span class='grey'>/" + snob + "</span></td>";
						}
					}
					else {
						zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='Brak miejsc w zagrodzie'><span class='grey'>0/" + snob + "</span></td>";
					}
				}
				else {
					zeile += "<td style='text-align:right; padding-left:5px; padding-right:5px; cursor:help;' title='Nie można wyprodukować więcej szlachciców'><span class='grey'>" + snob + "</span></td>";
				}
			}

			if(info == 2) {
				zeile += "<td style='text-align:left; padding:2px; cursor:default;'>" + bauauftrag + "</td>";
				zeile += "<td style='text-align:left; padding:2px; cursor:default;'>" + forschung + "</td>";
				zeile += "<td style='text-align:left; padding:2px; cursor:default;'>" + rekrutierung + "</td>";
			}


			// Aktualizacja stopki:
			document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].getElementsByTagName("tr")[i].innerHTML = zeile;
			if(i == zeilen-1) {
				if(snob_gesamt_2 != 1) {
					plus_snob = "c";
				}
	
				if(snob_gesamt_2 > 1) {
					plus_snob = "ców";
				}
	
				var inhalt = "";
				inhalt += "<tr>";
				if(GM_getValue("Speicher-Zus-" + welt) == "an") {
					inhalt += "<th colspan='3' style='text-align:center; padding:2px; cursor:default; -moz-border-radius-bottomleft:10px; -moz-border-radius-bottomright:10px;'>Informacje ogółem</th>";
				}
				else {
					inhalt += "<th colspan='2' style='text-align:center; padding:2px; cursor:default; -moz-border-radius-bottomleft:10px; -moz-border-radius-bottomright:10px;'>Informacje ogółem</th>";
				}
				
				// Surowce razem:
				// Drewno
				inhalt += "<th style='text-align:center; padding:2px; cursor:default; -moz-border-radius-bottomleft:10px;'><img src='/graphic/holz.png' title='Drewno' alt='' /></th>";
				if(holz_gesamt >= 1000000) {
					holz_gesamt = (holz_gesamt/1000000).toFixed(2) + " <small class='grey'>Mln.</small>";
				}
				else if((holz_gesamt < 1000000) && (holz_gesamt >= 1000)) {
					holz_gesamt = (holz_gesamt/1000).toFixed(3);
				}
				inhalt += "<th style='text-align:right; padding:2px; cursor:default; -moz-border-radius-bottomright:10px;'>" + holz_gesamt + "</th>";


				// Glina
				inhalt += "<th style='text-align:center; padding:2px; cursor:default; -moz-border-radius-bottomleft:10px;'><img src='/graphic/lehm.png' title='Glina' alt='' /></th>";
				if(lehm_gesamt >= 1000000) {
					lehm_gesamt = (lehm_gesamt/1000000).toFixed(2) + " <small class='grey'>Mln.</small>";
				}
				else if((lehm_gesamt < 1000000) && (lehm_gesamt >= 1000)) {
					lehm_gesamt = (lehm_gesamt/1000).toFixed(3);
				}
				inhalt += "<th style='text-align:right; padding:2px; cursor:default; -moz-border-radius-bottomright:10px;'>" + lehm_gesamt + "</th>";


				// Żelazo:
				inhalt += "<th style='text-align:center; padding:2px; cursor:default; -moz-border-radius-bottomleft:10px;'><img src='/graphic/eisen.png' title='Żelazo' alt='' /></th>";
				if(eisen_gesamt >= 1000000) {
					eisen_gesamt = (eisen_gesamt/1000000).toFixed(2) + " <small class='grey'>Mln.</small>";
				}
				else if((eisen_gesamt < 1000000) && (eisen_gesamt >= 1000)) {
					eisen_gesamt = (eisen_gesamt/1000).toFixed(3);
				}
				inhalt += "<th style='text-align:right; padding:2px; cursor:default; -moz-border-radius-bottomright:10px;'>" + eisen_gesamt + "</th>";


				// Spichlerz:
				if(speicher_gesamt >= 1000000) {
					speicher_gesamt = (speicher_gesamt/1000000).toFixed(2) + " <small class='grey'>Mln.</small>";
				}
				else if((speicher_gesamt < 1000000) && (speicher_gesamt >= 1000)) {
					speicher_gesamt = (speicher_gesamt/1000).toFixed(3);
				}
				inhalt += "<th colspan='2' style='text-align:right; padding:2px; cursor:default; -moz-border-radius-bottomleft:10px; -moz-border-radius-bottomright:10px;'>" + speicher_gesamt + "</th>";


				// Zagroda
				if(volk_gesamt_1 >= 1000000) {
					volk_gesamt_1 = (volk_gesamt_1/1000000).toFixed(2) + " <small class='grey'>Mln.</small>";
				}
				else if((volk_gesamt_1 < 1000000) && (volk_gesamt_1 >= 1000)) {
					volk_gesamt_1 = (volk_gesamt_1/1000).toFixed(3);
				}
				if(volk_gesamt_2 >= 1000000) {
					volk_gesamt_2 = (volk_gesamt_2/1000000).toFixed(2);
				}
				else if((volk_gesamt_2 < 1000000) && (volk_gesamt_2 >= 1000)) {
					volk_gesamt_2 = (volk_gesamt_2/1000).toFixed(3);
				}
				inhalt += "<th colspan='2' style='text-align:right; padding:2px; -moz-border-radius-bottomleft:10px; -moz-border-radius-bottomright:10px; cursor:default;'>" + volk_gesamt_1 + "/" + volk_gesamt_2 + "</th>";
				
				
				if((welt >= 3) && (GM_getValue("Speicher-GAG-" + welt) == "an")) {
					if(welt >= 10) {
						inhalt += "<th style='text-align:right; padding-left:5px; padding-right:5px; -moz-border-radius-bottomleft:10px; cursor:help;' title='Możesz w sumie " + gold_gesamt + " monet" + plus_gold + " wybić'>" + gold_gesamt + "</th>";
					}
					else {
						inhalt += "<th style='text-align:right; padding-left:5px; padding-right:5px; -moz-border-radius-bottomleft:10px; cursor:help;' title='Możesz w sumie " + gold_gesamt + " x surowców odłożyć'>" + gold_gesamt + "</th>";
					}
					
					if(snob_gesamt_1 != snob_gesamt_2) {
						inhalt += "<th style='text-align:right; padding-left:5px; padding-right:5px; -moz-border-radius-bottomright:10px; cursor:help;' title='Możesz maksymalnie " + snob_gesamt_2 + " z " + snob_gesamt_1 + " szlachci" + plus_snob + " wyprodukować'>" + snob_gesamt_2 + "<span class='grey'>/" + snob_gesamt_1 + "</span></th>";
					}
					else {
						inhalt += "<th style='text-align:right; padding-left:5px; padding-right:5px; -moz-border-radius-bottomright:10px; cursor:help;' title='Możesz maksymalnie " + snob_gesamt_2 + " szlachci" + plus_snob + " wyprodukować'>" + snob_gesamt_2 + "</th>";
					}
				}
				if(info == 2) {
					inhalt += "<th colspan='3' style='text-align:center; padding:2px; cursor:default; -moz-border-radius-bottomleft:10px; -moz-border-radius-bottomright:10px;'>Zlecenia</th>";
				}

				inhalt += "</tr>";
				document.getElementsByClassName("main")[main].getElementsByClassName("vis")[vis].innerHTML += inhalt;

				// Podświetlenie aktualnej wioski:
				var tabelle = document.getElementsByClassName("vis")[vis];
				var dorf_ist = document.getElementById("menu_row2").getElementsByTagName("a")[2].innerHTML;
				var zellen = tabelle.getElementsByTagName("tr")[1].getElementsByTagName("td").length;
				for(x=1; x<zeilen; x++) {
					var dorf_list = tabelle.getElementsByTagName("tr")[x].getElementsByTagName("td")[0].getElementsByTagName("a")[0].getElementsByTagName("span")[0].innerHTML.split(" (")[0];
					if((GM_getValue("Speicher-Akt-" + welt) == "an") && (dorf_list == dorf_ist)) {
						tabelle.getElementsByTagName("tr")[x].getElementsByTagName("td")[0].getElementsByTagName("a")[0].setAttribute("style", "color:#6DE377;");
					}
				}
			}
		}
	}
 }
}

vlgrvpatch()
